<?php

namespace app\admin\controller;

use app\admin\model\LinkModel;
use app\admin\model\SchoolModel;
use cmf\controller\AdminBaseController;
use think\console\input\Option;
use think\Db;
use think\db\Query;
use think\Request;
use think\Validate;

class SchoolController extends AdminBaseController
{

    public function index()
    {
      $name=$this->request->param('name');//获取表单数据
        $school=Db::name('school')
            ->where(function (Query $query)
            use($name){
                if ($name)
                $query->where('name','like',"%$name%");
            })
            ->order('id desc')
            ->paginate('3');
        $page=$school->render();
        return view('index',['school'=>$school,'page'=>$page]);
    }


    public function add(){
        $province = Db::name('area')
            ->where('level',1)
            ->select();
        if (request()->isPost()){
            $data=input();
            $rule = [
                'name' => 'require',
                'province' => 'require',
                'city' => 'require',
                'area' => 'require'
            ];
            $msg = [
                'name.require' => '学校名称不能为空',
                'province.require' => '请选择省份',
                'city.require' => '请选择城市',
                'area.require' => '请选择县区'
            ];
            $validate = Validate::make($rule,$msg);
            $result = $validate->check($data);
            if (!$result){
                $this ->error($validate->getError());
            }
            $add=Db::name('school')->insert($data);
            if($add){
                $this->success('添加成功','school/index');
            }else{
                $this->error('添加失败','school/index');
            }
        }
        return view('add',['province'=>$province]);
    }
    public function getCity(){
        $areaId = input('areaId');
        $city = Db::name('area')
            ->where('parentId',$areaId)
            ->where('level',2)
            ->select();
//          halt($city);
          $cityArr=[
              '<option value=""> 请选择</option> '
          ];
          foreach ($city as $v){
              $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option> ";
              //halt($opt);
              array_push($cityArr,$opt);
          }
          return $cityArr;
    }
    public function getArea()
    {
        $cityId=input('cityId');
        $area=Db::name('area')
            ->where('parentId',$cityId)
            ->where('level',3)
            ->select();
            //halt($area);
        $areaArr=[
            '<option value="">请选择</option>'
        ];
        foreach ($area as $v){
            $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($areaArr,$opt);
        }
        return $areaArr;
    }

    public function edit()
    {
        $province = Db::name('area')
            ->where('level',1)
            ->select();
        $city = Db::name('area')
            ->where('level',2)
            ->select();
        $area = Db::name('area')
            ->where('level',3)
            ->select();
        //halt($area);
        //halt($city);
        if(request()->isPost()){
            $data=input();
            $edit=SchoolModel::update($data,['id'=>$data['id']]);
            if ($edit){
                $this->success('修改成功',url('school/index'));
            }else{
                $this->error('修改失败',url('school/index'));
            }
        }
        $id=input('id');
        $school=SchoolModel::get($id);
        return $this->fetch('edit',['data'=>$school,'province'=>$province,'city'=>$city,'area'=>$area]);
    }
    public function delete($id){
        $del=Db::name('school')->where('id',$id)->delete();
        if ($del){
            $this->success('删除成功',url('school/index'));
        }else{
            $this->error('删除失败');
        }
    }

}
